package com.moinut.studentattendancemanager.controller;

import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.factory.DAOFactory;
import com.moinut.studentattendancemanager.model.vo.AttendanceInfo;
import com.sun.istack.internal.NotNull;

import java.util.List;

/**
 * @author MOILING
 */
public class AttendanceController {

    public static void getAttendancesInfoByCourseId(@NotNull String courseId, @NotNull OnGetListListener<AttendanceInfo> listener) {
        if (courseId.isEmpty()) {
            listener.onError("课程号不能为空");
            return;
        }
        try {
            List<AttendanceInfo> attendancesInfo = DAOFactory.getIAttendanceInfoDAOInstance().getAttendancesInfoByCourseId(courseId);
            if (attendancesInfo.size() > 0) {
                listener.onSuccess(attendancesInfo);
            } else {
                listener.onError("没有考勤记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAttendancesInfoByCourseIdAndStudentId(@NotNull String courseId, @NotNull String studentId, @NotNull OnGetListListener<AttendanceInfo> listener) {
        if (courseId.isEmpty()) {
            listener.onError("课程号不能为空");
            return;
        }
        try {
            List<AttendanceInfo> attendancesInfo = DAOFactory.getIAttendanceInfoDAOInstance().getAttendancesInfoByCourseIdAndStudentId(courseId, studentId);
            if (attendancesInfo.size() > 0) {
                listener.onSuccess(attendancesInfo);
            } else {
                listener.onError("没有考勤记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAllAttendancesInfo(@NotNull OnGetListListener<AttendanceInfo> listener) {
        try {
            List<AttendanceInfo> attendancesInfo = DAOFactory.getIAttendanceInfoDAOInstance().getAllAttendancesInfo();
            if (attendancesInfo.size() > 0) {
                listener.onSuccess(attendancesInfo);
            } else {
                listener.onError("没有考勤记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAttendancesInfoByTeacherId(@NotNull String teacherId, @NotNull OnGetListListener<AttendanceInfo> listener) {
        try {
            List<AttendanceInfo> attendancesInfo = DAOFactory.getIAttendanceInfoDAOInstance().getAttendancesInfoByTeacherId(teacherId);
            if (attendancesInfo.size() > 0) {
                listener.onSuccess(attendancesInfo);
            } else {
                listener.onError("没有考勤记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void updateAttendanceInfo(@NotNull AttendanceInfo attendanceInfo, @NotNull OnUpdateListener listener) {
        try {
            if (DAOFactory.getIAttendanceInfoDAOInstance().updateAttendanceInfo(attendanceInfo)) {
                listener.onSuccess("修改成功");
            } else {
                listener.onSuccess("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void insertAttendanceInfo(@NotNull AttendanceInfo attendanceInfo, @NotNull OnUpdateListener listener) {
        try {
            if (DAOFactory.getIAttendanceInfoDAOInstance().insertAttendanceInfo(attendanceInfo)) {
                listener.onSuccess("添加成功");
            } else {
                listener.onSuccess("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }
}
